package com.example.leetcode.tree;

import com.example.leetcode.tree.common.TreeNode;
import com.example.leetcode.tree.common.TreeUtils;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode(int x) { val = x; }
 * }
 */
class Solution285 {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        return TreeUtils.inorderSuccessor(root, p);
    }
}

public class leetcode285 {
    public static void main(String[] args) {
        Solution285 solution285 = new Solution285();
        Integer[] nums = {5, 3, 6, 2, 4, null, null, 1};
        test285(solution285, nums, 6);
        test285(solution285, nums, 3);
    }

    private static void test285(Solution285 solution285, Integer[] nums, int val) {
        TreeNode root = TreeUtils.generateLeetcodeTree(nums);
        TreeNode node = TreeUtils.getTreeNodeByVal(root, val);
        TreeNode inorderSuccessor = solution285.inorderSuccessor(root, node);
        System.out.println(inorderSuccessor == null ? null : inorderSuccessor.val);
    }
}
